<?php
/**
 * fldcms
 * ============================================================================
 * 版权所有 2017 福建华晟网络科技，并保留所有权利。
 * 网站地址: http://www.fldcms.cn
 * ----------------------------------------------------------------------------
 * 这不是一个自由软件！您只能在不用于商业目的的前提下对程序代码进行修改和使用 .
 * 不允许对程序代码以任何形式任何目的的再发布。
 * ============================================================================
 * Author: fld
 * Date: 2017-08-09
 */
namespace app\admin\controller;
use think\Request;
/**
 * 节点管理
 * @author fld
 *
 */
class NodeController extends BaseController{
	function __construct(){
		parent::__construct();
		$this->db = model('Node');
	}
	/**
	 * 节点列表
	 */
	public function indexOp(){
		$node_list = $this->db->getNodeList(array('level'=>1));
		foreach ($node_list as $k=>$v){
			$children_list = $this->db->getNodeList(array('pid'=>$v['node_id']));
			$node_list[$k]['c_list'] = $children_list;
			$node_list[$k]['c_count'] = count($children_list);
		}
		$this->assign('list',$node_list);
		return $this->fetch();
	}
	/**
	 * 获取下级节点信息
	 */
	public function getNodeOp(){
		$pid = input('param.pid');
		$node_list = $this->db->getNodeList(array('pid'=>$pid));
		foreach ($node_list as $k=>$v){
			$children_list = $this->db->getNodeList(array('pid'=>$v['node_id']));
			$node_list[$k]['c_list'] = $children_list;
			$node_list[$k]['c_count'] = count($children_list);
		}
		return json($node_list);
	}
	/**
	 * 添加节点信息
	 */
	public function addOp(){
	    if (chksubmit()){
	    	$result = array();
	        $model = model('base');
	        $pid = input('post.pid');
	        $node_info = $model->find_table('node',array('node_id'=>$pid),'level');
	        $_POST['level'] = intval($node_info['level'])+1;
	        $res = $model->add_table('node',$_POST);
	        if($res !== false){
	            model('ActionLog')->action_log("添加","添加节点成功，节点编号=".$res."，节点名称=".$_POST['node_title']);
	            $this->msg = "添加节点成功";
	            $result['error'] = 0;
	        }else{
	            $this->msg = "添加节点失败";
	            $result['error'] = 1;
	        }
	        $result['msg'] = $this->msg;
	        return json($result);
	    }else{
	    	//父类节点选择
	        $node_list = $this->db->getNodes(0, 1);
	        $this->assign('node_list',$node_list);
	        return $this->fetch();
	    }
	}
	/**
	 * 编辑节点信息
	 */
	public function editOp(){
		$model = model('base');
		if (chksubmit()){
			$result = array();
			$node_id = input('post.node_id');
			$pid = input('post.pid');
			$node_info = $model->find_table('node',array('node_id'=>$pid),'level');
			$_POST['level'] = intval($node_info['level'])+1;
			$res = $model->update_table('node',array('node_id'=>$node_id),$_POST);
			if($res !== false){
				model('ActionLog')->action_log("编辑","编辑节点成功，节点编号=".$res."，节点名称=".$_POST['node_title']);
				$this->msg = "编辑节点成功";
				$result['error'] = 0;
			}else{
				$this->msg = "编辑节点失败";
				$result['error'] = 1;
			}
			$result['msg'] = $this->msg;
			return json($result);
		}else{
			//父类节点选择
			$node_list = $this->db->getNodes(0, 1);
			$this->assign('node_list',$node_list);
			//节点信息
			$node_id = input('param.node_id');
			$node_info = $model->find_table('node',array('node_id'=>$node_id));
			$this->assign('vo',$node_info);
			return $this->fetch();
		}
	}
	/**
	 * 删除节点信息
	 */
	public function deleteOp(){
		$result = array();
		$node_id = input('param.node_id');
		$model = model('base');
		$node_info = $model->find_table('node',array('node_id'=>$node_id),'node_id,node_title');
		$res = $model->delete_table('node',array('node_id'=>$node_id));
		if($res !== false){
			model('ActionLog')->action_log("删除","删除节点成功，节点编号=".$node_info['node_id']."，节点名称=".$node_info['node_title']);
			$result['error'] = 0;
		}else{
			$this->msg = "删除节点失败";
			$result['error'] = 1;
		}
		$result['msg'] = $this->msg;
		return json($result);
	}
}